package app.widget;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import app.adapter.TableAdapter;

import com.longevityassistant.activity.R;

/**
 * 自定义固定TableView，不需要滚动
 * 
 * @author EX-ZHENGDUANCHUAN001
 * 
 */
public class FixedTableView extends LinearLayout {

	/**
	 * 观察者
	 */
	private AdapterDataSetObserver mDataSetObserver;
	/**
	 * 适配器
	 */
	private TableAdapter mAdapter;
	/**
	 * 单项单击监听
	 */
	private OnItemClickListener mOnItemClickListener;
	
	/**
	 * 是否添加默认底部
	 */
	private boolean mIsShowFooter = true;

	/**
	 * 构造方法
	 * @param context 上下文
	 * @param attrs 属性
	 */
	public FixedTableView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	/**
	 * 构造方法
	 * @param context 上下文
	 */
	public FixedTableView(Context context) {
		super(context);
	}

	/**
	 * 初始化
	 */
	private void init() {
		setOrientation(LinearLayout.VERTICAL);
	}

	/**
	 * 设置适配器
	 * 
	 * @param adapter adapter
	 */
	public void setAdapter(TableAdapter adapter) {
		if (adapter == null) {
			return;
		}
		if (null != mAdapter) {
			mAdapter.unregisterDataSetObserver(mDataSetObserver);
		}
		resetList();
		mAdapter = adapter;
		mDataSetObserver = new AdapterDataSetObserver();
		mAdapter.registerDataSetObserver(mDataSetObserver);

		View headerView = adapter.getHeaderView();
		addView(headerView);
		initItem();
		View footerView = adapter.getFooterView();
		if (footerView != null) {
			footerView.setOnClickListener(null);
			addView(footerView);
		}
		if (mIsShowFooter) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View fv = inflater.inflate(R.layout.app_widget_table_adapter_footer_bottom, null);
			fv.setOnClickListener(null);
			addView(fv);
		}
	}

	/**
	 * 重设view
	 */
	private void resetList() {
		removeAllViews();
	}

	/**
	 * 初始化项
	 */
	private void initItem() {
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		for (int i = 0; i < mAdapter.getCount(); i++) {
			final View itemView = mAdapter.getView(i, null, null);
			itemView.setLayoutParams(lp);
			addView(itemView);

			final int position = i;
			itemView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mOnItemClickListener != null) {
						mOnItemClickListener.onItemClick(null, itemView, position, 0);
					}
				}
			});
		}

		requestLayout();
	}

	/**
	 * 数据改变观察者
	 * @author EX-ZHENGDUANCHUAN001
	 *
	 */
	private class AdapterDataSetObserver extends DataSetObserver {

		@Override
		public void onChanged() {
			setAdapter(mAdapter);
		}

		@Override
		public void onInvalidated() {
		}
	}

	/**
	 * list项单击事件
	 * 
	 * @param listener listener
	 */
	public void setOnItemClickListener(OnItemClickListener listener) {
		mOnItemClickListener = listener;
	}

	/**
	 * 刷新view
	 */
	public void refresh() {
		setAdapter(mAdapter);
	}

	/**
	 * 是否添加默认底部
	 * @param isShow isShow
	 */
	public void showfooter(boolean isShow) {
		mIsShowFooter = isShow;
	}
}
